package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseConText;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 订单管理
 */
@RestController
@RequestMapping("order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private UserService userService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @Transactional
   @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){

        orderService.submit(orders);
        return R.success("下单成功");
    }
    @GetMapping("/userPage")
    public R<Page<OrdersDto>>userPage(Integer page, Integer pageSize, String number){
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(number),"number",number);
        queryWrapper.orderByAsc("order_time");
        orderService.page(pageInfo,queryWrapper);
        Page<OrdersDto> pageInfo2 = new Page<>(page, pageSize);
        ArrayList<OrdersDto> ordersDtos = new ArrayList<>();
        List<Orders> records = pageInfo.getRecords();
        records.forEach(orders -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(orders,ordersDto);
            Long ordersId = orders.getId();
            QueryWrapper<OrderDetail> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("order_id",ordersId);
            List<OrderDetail> list = orderDetailService.list(queryWrapper1);
            ordersDto.setOrderDetails(list);
            ordersDtos.add(ordersDto);
        });
        BeanUtils.copyProperties(pageInfo,pageInfo2);
        pageInfo2.setRecords(ordersDtos);
        return R.success(pageInfo2);
    }
    @GetMapping("/page")
    public R<Page<Orders>>page(
            Integer page,
            Integer pageSize,
            String number,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date beginTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date endTime
            ){
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(number),Orders::getNumber,number);
        if (beginTime != null){
            queryWrapper.between(Orders::getOrderTime,beginTime,endTime);
        }
        orderService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }
    @PutMapping
    public R<String>update(@RequestBody Orders orders){
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getId,orders.getId());
        queryWrapper.eq(Orders::getStatus,orders.getStatus());
        orderService.update(queryWrapper);
        return R.success("修改成功");
    }
    @PostMapping("/again")
    public R<String>payAgain(@RequestBody Orders orders){
        List<OrderDetail> orderDetails = orderDetailService.list(new QueryWrapper<OrderDetail>()
                .eq("order_id", orders.getId()));
        orderDetails.forEach(orderDetail -> {
            ShoppingCart cart = new ShoppingCart();
            BeanUtils.copyProperties(orderDetail,cart,"id");
            cart.setUserId(BaseConText.getCurrentId());
            shoppingCartService.save(cart);
        });
        return R.success("成功");
    }
}